//Possibly mave some of this to a DB as well.

require( "datastream" )
require( "mysql" )
require( "glon" )

EnableDisplayLog = true
ServerName = "Pleasurable Obituary [PO] Build Server"
MaxLogTable = 150

//How often to save player data. (Time and Points, etc.) IN SECONDS
SaveInterval = 180

//How often you gain a point. (Change this later to depend on how long (RANKED) the person has been there.
//In seconds
PointInterval = 150

//Notice Interval
NoticeInterval = 90

//Table of command info strings
CINFOStrings = {}	//Work on these, tooltips for the button type commands POSSBILY the other types as well
CINFOStrings["Hurt_Player"] = "Hurt the selected player with the entered amount of damage."
CINFOStrings["Toggle_Godmode"] = "Toggle God mode on or off."
CINFOStrings["Strip_Weapons"] = "Strip the weapons of the player."
CINFOStrings["Rocket_Player"] = "Rocket the player."
CINFOStrings["Set_Rank"] = "Set the rank of the player."
CINFOStrings["Toggle_Player_NoClip"] = "Toggle the allowance of the selected player's NoClip."
CINFOStrings["Kick_Player"] = "Kick the player with the specified reason."
CINFOStrings["Ban_Player"] = "Ban the player for the specified time with the specified reason"
CINFOStrings["Add_Points"] = "Modify the points of the selected player"


if CLIENT then		//Let's use this so we can allow functions in the command defining. Just set these variables from the AMenu

SelSID = ""
HurtAM = 1

end


//Commands
ModCommands = {}
ModCommands[1] = "Hurt_Player"
ModCommands[2] = "Toggle_Godmode"
ModCommands[3] = "Strip_Weapons"
ModCommands[4] = "Rocket_Player"
ModCommands[5] = "Toggle_Player_NoClip"
ModCommands[6] = "Jail_Player"
ModCommands[7] = "Unjail_Player"

AdminCommands = {}
AdminCommands[1] = "Notices_Menu"
AdminCommands[2] = "ChangeMap_Menu"
//AdminCommands[1] = "Kick_Player"
//AdminCommands[2] = "Ban_Player"

OwnerCommands = {}
OwnerCommands[1] = "Rebuild_Store_Tables"
OwnerCommands[2] = "PP_Limits"
//OwnerCommands[1] = "Set_Rank"

//These will be chat based
MemberCommands = {}
MemberCommands[1] = "tp"
MemberCommands[2] = "bring"
MemberCommands[3] = "goto"


RankTable = {}
TimesTable = {}
ColorTable = {}
RankNums = {}	//Change this to the ID table on the DB
ReadNotices = {}

if SERVER then

function RebuildRanksTable()
	local DBRanks = RANKDBINFO()
	
	for k,v in pairs( DBRanks ) do
		RankTable[k] =  DBRanks[k][2]
		TimesTable[k] = DBRanks[k][3]
		RankNums[DBRanks[k][2]] = DBRanks[k][5]
	end
	table.insert( TimesTable, -1 )
	
	for k,v in pairs( DBRanks ) do
		local cr, cg, cb, ca = 0
		for w in string.gmatch( DBRanks[k][4], "r=.-%," ) do
			local ToR = string.Replace( w, "r=", "" )
			ToR = string.Replace( ToR, ",", "" )
			cr = tonumber(ToR)
		end
		for w in string.gmatch( DBRanks[k][4], "g=.-%," ) do
			local ToG = string.Replace( w, "g=", "" )
			ToG = string.Replace( ToG, ",", "" )
			cg = tonumber(ToG)
		end
		for w in string.gmatch( DBRanks[k][4], "b=.-%," ) do
			local ToB = string.Replace( w, "b=", "" )
			ToB = string.Replace( ToB, ",", "" )
			cb = tonumber(ToB)
		end
		for w in string.gmatch( DBRanks[k][4], "a=.*" ) do
			local ToA = string.Replace( w, "a=", "" )
			ToA = string.Replace( ToA, ",", "" )
			ca = tonumber(ToA)
		end
		ColorTable[RankTable[k]] = Color( cr, cg, cb, ca )
	end
	
	local DATA = { RankTable, TimesTable, ColorTable, RankNums }
	datastream.StreamToClients( player.GetAll(), "RanksStream", DATA )
	
end
hook.Add( "Initialize", "RebuildRanksTable", RebuildRanksTable )

function RebuildNotices()
	if !file.IsDir( "xqam" ) then
		file.CreateDir( "xqam" )
	end
	
	if file.Exists( "xqam/Notices.txt" ) then
		ReadNotices = glon.decode( file.Read( "xqam/Notices.txt" ) )
	else
		ReadNotices = {}
		file.Write( "xqam/Notices.txt", glon.encode( ReadNotices ) )
	end

end
hook.Add( "Initialize", "RebuildNotices", RebuildNotices )

function StreamNotices( ply, command, args)
	datastream.StreamToClients( { ply }, "NoticesStream", { ReadNotices } )
end
concommand.Add( "StreamNotices", StreamNotices )

function SaveNotices( ply, handle, id, encoded, decoded )
	ReadNotices = decoded[1]
	PrintTable( ReadNotices )
	//Message players letting them know notices were updated
	file.Write( "xqam/Notices.txt", glon.encode(ReadNotices) )
end
datastream.Hook( "SaveNoticesStream", SaveNotices )
	
function StreamRanks( ply )
	local DATA = { RankTable, TimesTable, ColorTable, RankNums }
	datastream.StreamToClients( { ply }, "RanksStream", DATA )
	
	ply:PrintMessage( HUD_PRINTTALK, "Receiving Rank Information." )
	ply:SetNWInt( "nstream", 0 )
end
hook.Add("PlayerInitialSpawn", "StreamRanks", StreamRanks)

function NotifyStreamReceived( ply, command, args )
	ply:PrintMessage( HUD_PRINTTALK, args[1] )
end
concommand.Add( "NotifyStreamReceived", NotifyStreamReceived )

end

if CLIENT then
CanScoreboard = false
CanF2 = false

function RankStreamHook( pl, handle, id, encoded, decoded )
	RankTable = encoded[1]
	TimesTable = encoded[2]
	ColorTable = encoded[3]
	RankNums = encoded[4]
end
datastream.Hook( "RanksStream", RankStreamHook )

function NoticesStreamHook( pl, handle, id, encoded, decoded )
	ReadNotices = encoded[1]
end
datastream.Hook( "NoticesStream", NoticesStreamHook )

function CompletedStreams( handler, id, rawdata, procdata )
	if handler == "NoticesStream" then LocalPlayer():SetNWInt( "nstream", 1 ) print( "NOTICES COMPLETED" ) end
	
	if handler == "RanksStream" then timer.Simple( 2, function() CanScoreboard = true RunConsoleCommand( "NotifyStreamReceived", "Rank Information Received" ) end ) print( "PLAYER RECEIVED RANKS" ) end
	
	if handler == "StoreStream" then timer.Simple( 2, function() CanF2 = true RunConsoleCommand( "NotifyStreamReceived", "Store Information Receieved." ) end ) print( "PLAYER RECEIVED STORE" ) end
	
	if handler == "MAPSTREAM" then CanMCHANGE = true end
end
hook.Add( "CompletedIncomingStream", "CompletedStreams", CompletedStreams )

end


//Table for adding Flag Options to the Admin Menu
FlagTable = { "N", "T", "D", "M", "A", "O", "J" }


